package com.test.sdk.receive.mq;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;
import org.springframework.amqp.core.Message;
/**
 * @author 徒有琴
 */
@Component
@RabbitListener(queues = "test1")//标记当前类是用来处理来自于这个队列的消息的
public class MessageListener {
    @RabbitHandler//标记当前方法是用来处理消息的
    public void receive(String key, Channel channel, Message message) {
        String messsageText = new String(message.getBody());
        System.out.println("[receiver] receive message : " + messsageText);
        //接收到订单号之后，根据业务处理订单
        try {
            //手动接收应答，不会在发送这条消息
            System.out.println("[receiver] confirm");
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}